//
//  TeacherViewController.m
//  GC_OC
//
//  Created by 曾 on 2022/1/17.
//

#import "TeacherViewController.h"
#import "TeacherHeaderView.h"
@interface TeacherViewController ()
@property (nonatomic, strong)ObjectAFNetWorking* objectAFNet;
@property (nonatomic, strong)TeacherHeaderView*  titleView;
@property (nonatomic, strong)NSDictionary*      myDic;
@end

@implementation TeacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeadView];
    self.view.backgroundColor=RBG(247,247,247);
    self.objectAFNet=[[ObjectAFNetWorking alloc]init];
    [self getData];
    
}
-(void)getData
{
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat: @"/Documents/%@_AudionList.json",self.dic[@"teacher_id"]]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData* dataString=[NSData dataWithContentsOfFile:filePath];
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        self.myDic=dic;
        [self upDateView];
        [GC_OCTool saveFileAction:filePath];
    }else
    {
        [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/index/AudioList?keyword=%@",server_addr,self.dic[@"teacher_id"]] finish:^(id  _Nonnull dataString) {
            NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
            NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat: @"/Documents/%@_AudionList.json",self.dic[@"teacher_id"]]];
            if ([fileManager fileExistsAtPath:filePath]) {
                [fileManager removeItemAtPath:filePath error:nil];
            }
            [dataString writeToFile:filePath atomically:YES];
            self.myDic=dic;
            [self upDateView];
        } andError:^(NSError * _Nonnull error) {
        }];
    }
}
-(void)upDateView
{
    [_titleView removeFromSuperview];
    _titleView=nil;
    [self.view addSubview:self.titleView];
}
-(TeacherHeaderView*)titleView
{
    if(!_titleView)
    {
        _titleView=[[TeacherHeaderView alloc]initWithFrame:CGRectMake(0,topHeight,SCREEN_WIDTH,wid(232)) andDic:self.myDic[@"data"][@"teacher"]];
    }
    return _titleView;
}
-(void)creatHeadView
{
    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,topHeight)];
    view.backgroundColor=AppMainColor;
    CGFloat titleHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)] andWidth:wid(100)];
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(65),StatusBarHeight+(topHeight-StatusBarHeight-titleHeight)/2.0,SCREEN_WIDTH-wid(65)*2,titleHeight)];
    titleLabel.textColor=[UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)]];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=@"教师信息";
    [view addSubview:titleLabel];
    
    UIButton* leftbtn=[UIButton new];
    leftbtn.frame=CGRectMake(wid(20),StatusBarHeight+(topHeight-StatusBarHeight-wid(40))/2.0,wid(40),wid(40));
    [leftbtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftbtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [leftbtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftbtn];
    [self.view addSubview:view];
    
}
-(void)leftBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
    if (self.navigationController.viewControllers.count == 1) {
        if ([ICMusicPlayManager sharedManager].narrowView) {
            [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - GCtabbarHeight, SCREEN_WIDTH, 50);
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
